

package com.zf.yichat.controller.packet;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.mapper.PacketVipRuleApiMapper;
import com.zf.yichat.mapper.PacketVipRuleMapper;
import com.zf.yichat.mapper.SysDictDataMapper;
import com.zf.yichat.model.PacketVipRule;
import com.zf.yichat.model.SysDictData;
import com.zf.yichat.service.config.RedisService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 *
 */
@Controller
@RequestMapping("packet/integral/rule")
public class PacketIntegralRuleController extends BaseController {
    @Resource
    PacketVipRuleMapper packetVipRuleMapper;
    @Resource
    PacketVipRuleApiMapper packetVipRuleApiMapper;
    @Resource
    SysDictDataMapper sysDictDataMapper;
    @Autowired
    RedisService redisService;

    @RequestMapping("/index")
    public String index() {
        return "packet/integral-rule-index";
    }

    @RequestMapping("save/index")
    public String saveIndex(Long id) {
        getRequest().setAttribute("packetIntegralRule", packetVipRuleMapper.selectByPrimaryKey(id));
        Example example = new Example(SysDictData.class);
        example.createCriteria().andEqualTo("pcode", "integral_level");
        getRequest().setAttribute("levelMap", DtoChangeUtils.getMap(sysDictDataMapper.selectByExample(example),
                SysDictData::getName, SysDictData::getId
        ));
        return "packet/integral-rule-save";
    }

    @RequestMapping("list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(packetVipRuleApiMapper.selectList(), v -> v);
    }


//    @RequestMapping("status/save")
//    @ResponseBody
//    public FsResponse setStatus(Integer status, Long id) {
//
//        PacketVipRule rule = packetVipRuleMapper.selectByPrimaryKey(id);
//        Contracts.assertNotNull(rule, YiChatMsgCode.SYSTEM_ERROR.msg());
//        rule.setStatus(status);
//        if (rule.getStatus() == 1) {
//            //只有一个生效的，将其他生效变未生效
//            Example example = new Example(PacketVipRule.class);
//            example.createCriteria().andEqualTo("status", 1).andEqualTo("dictDataId", rule.getDictDataId());
//            PacketVipRule rule1 = new PacketVipRule();
//            rule1.setStatus(0);
//            packetVipRuleMapper.updateByExampleSelective(rule1, example);
//
//            //更新规则并生效配置
//            packetVipRuleMapper.updateByPrimaryKeySelective(rule);
//            redisService.deleteAllPacketVip();
//        } else {
//            packetVipRuleMapper.updateByPrimaryKeySelective(rule);
//        }
//        return FsResponseGen.success();
//    }

//    @RequestMapping("force/save")
//    @ResponseBody
//    public FsResponse setForce(Integer forceValid, Long id) {
//
//        PacketVipRule rule = new PacketVipRule();
//        rule.setForceValid(forceValid);
//        rule.setId(id);
//        packetVipRuleMapper.updateByPrimaryKeySelective(rule);
//        return FsResponseGen.success();
//    }

    @RequestMapping("save")
    @ResponseBody
    public FsResponse save(PacketVipRule rule) {
        int total = rule.getTotalMoney().multiply(new BigDecimal("100")).intValue();
        int max = rule.getMaxMoney().multiply(new BigDecimal("100")).intValue();
        int min = rule.getMinMoney().multiply(new BigDecimal("100")).intValue();
        int splitNum = rule.getNum();
        Contracts.assertTrue(total >= splitNum * min && total <= splitNum * max, "最小最大值配置错误");
        //强制生效
        if (rule.getForceValid() == 1) {
            redisService.deleteAllPacketVip();
        }
        rule.setStatus(1);
        packetVipRuleMapper.updateByPrimaryKeySelective(rule);
        return FsResponseGen.success();
    }
}
